package com.topnav.mc.admin.view.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.topnav.mc.admin.security.PermissionGrade;
import com.topnav.mc.base.CommViewServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class UserViewServiceImpl extends CommViewServiceImpl<UserViewMapper,UserView> implements IUserViewService {


    /**
     * 由实体类生成查询条件
     * 并返回QueryWrapper
     * @param queryWrapper
     * @return
     */
    @Override
    public QueryWrapper<UserView> genWrapper(QueryWrapper<UserView> queryWrapper){
        PermissionGrade permissionGrade =  this.getPermissionGrade();

        /**
         * 已生成条件查諙
         */
        if (queryWrapper.getEntity()==null){
            if (permissionGrade != null){
                if(permissionGrade.getPermissionLevel() == 1){
                    queryWrapper.eq("id",permissionGrade.getUserId());
                }else if(permissionGrade.getPermissionLevel() == 2){
                    queryWrapper.eq("org_id",permissionGrade.getOrgId());
                }else if(permissionGrade.getPermissionLevel() == 3 || permissionGrade.getPermissionLevel() == 4){
                    queryWrapper.like("path",permissionGrade.getOrgPath());
                }
            }
            return queryWrapper;
        }
        /**
         * 按实体类生成的条件查询
         */
        QueryWrapper<UserView> wrapper = new QueryWrapper<UserView>();
        if (permissionGrade != null){
            if(permissionGrade.getPermissionLevel() == 1){
                wrapper.eq("id",permissionGrade.getUserId());
            }else if(permissionGrade.getPermissionLevel() == 2){
                wrapper.eq("org_id",permissionGrade.getOrgId());
            }else if(permissionGrade.getPermissionLevel() == 3 || permissionGrade.getPermissionLevel() == 4){
                wrapper.like("path",permissionGrade.getOrgPath());
            }
        }
        if (!StringUtils.isEmpty(wrapper.getEntity().getLoginName())){
            wrapper.like("login_name", wrapper.getEntity().getLoginName());
        }
        if (!StringUtils.isEmpty(wrapper.getEntity().getName())){
            wrapper.like("name", wrapper.getEntity().getName());
        }
        if (!StringUtils.isEmpty(wrapper.getEntity().getCode())){
            wrapper.like("code", wrapper.getEntity().getCode());
        }
        if (!StringUtils.isEmpty(wrapper.getEntity().getOrgId())){
            wrapper.eq("org_id", wrapper.getEntity().getOrgId());
        }
        return wrapper;
    }
}
